package org.example.day04;

import java.util.ArrayList;
import java.util.List;

public class MyThread9 {
    private List list = new ArrayList();
    synchronized public void add(String username){
        System.out.println("ThreadName = " + Thread.currentThread().getName() +  " 执行了add方法");
        list.add(username);
        System.out.println("ThreadName = " + Thread.currentThread().getName() +  " 退出了add方法");
    }
    synchronized public int getSize(){
        System.out.println("ThreadName = " + Thread.currentThread().getName() +  " 执行了getSize方法");
        int size = list.size();
        System.out.println("ThreadName = " + Thread.currentThread().getName() +  " 退出了getSize方法");
        return size;
    }

    public static void main(String[] args) {
        MyThread9 m9 = new MyThread9();
        Thread9A ta = new Thread9A(m9);
        ta.setName("A");
        ta.start();
        Thread9B tb = new Thread9B(m9);
        tb.setName("B");
        tb.start();
    }
}

class Thread9A extends Thread{
    private MyThread9 list;

    public Thread9A(MyThread9 list) {
        this.list = list;
    }

    @Override
    public void run() {
        for (int i = 0; i < 100000; i++) {
            list.add("threadA" + (i+1));
        }
    }
}
class Thread9B extends Thread{
    private MyThread9 list;

    public Thread9B(MyThread9 list) {
        this.list = list;
    }

    @Override
    public void run() {
        for (int i = 0; i < 100000; i++) {
            list.add("threadB" + (i+1));
        }
    }
}